﻿using DelonixRegia.Classes;
using DelonixRegia.Classes.Reporting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;

namespace DelonixRegia
{
    public partial class AllRoomGuest : System.Web.UI.Page
    {
        
        static string dateone ="";
        static string datetwo = "";
        static ArrayList roomlist = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            PDFBut.Visible = true;
            ExcelBut.Visible = true;
            PrintBut.Visible = true;
            // CHEW SENG'S CODE - DON'T TOUCH //
            Account a = (Account)Session["acc"];

            if (a != null)
            {
                if (a.userType < 2)
                    Response.Redirect("Default.aspx");
            }
            else
                Response.Redirect("SignIn.aspx");
            // CHEW SENG'S CODE - DON'T TOUCH //

            if(!Page.IsPostBack){
                Calendar1.SelectedDate = DateTime.Today;
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (Calendar1.SelectedDates.Count == 1)
            {
                roomlist = new ArrayList();
                string dtone = Calendar1.SelectedDate.ToShortDateString();
                string dtonetime = "00:00:00";
                DateTime dt1 = DateTime.Parse(dtone + " " + dtonetime);
                dateone = Calendar1.SelectedDate.ToShortDateString();
                string dttwo = Calendar1.SelectedDate.ToShortDateString();
                string dttwotime = "23:59:59";
                DateTime dt2 = DateTime.Parse(dttwo + " " + dttwotime);
                datetwo = Calendar1.SelectedDate.ToShortDateString();
                roomlist = reportingDBManager.getBookingDetailsByDates(dt1, dt2);

                gvAllGuests.DataSource = roomlist;
                gvAllGuests.DataBind();

                pnlFirstStep.Visible = false;
                pnlSecondStep.Visible = true;
                Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
                if (roomlist.Count == 0)
                {
                    ClientScriptMgr.Execute("$('#errorbox2').html('There are no guests in Delonix Regia at this particular date.');$('#errorbox2').hide().show();");
                    PDFBut.Visible = false;
                    ExcelBut.Visible = false;
                    PrintBut.Visible = false;
                }
            }
            else
            {
                roomlist = new ArrayList();
                ArrayList al = new ArrayList();
                foreach (DateTime dt in Calendar1.SelectedDates)
                {
                    al.Add(dt);
                }
                DateTime dtone = (DateTime)al[0]; //start
                string dt1date = dtone.ToShortDateString();
                string dtonetime = "00:00:00";
                DateTime dt1 = DateTime.Parse(dt1date + " " + dtonetime);
                dateone = dtone.ToShortDateString();

                DateTime dttwo = (DateTime)al[al.Count - 1]; //end
                string dt2date = dttwo.ToShortDateString();
                string dttwotime = "00:00:00";
                DateTime dt2 = DateTime.Parse(dt2date + " " + dttwotime);
                datetwo = dttwo.ToShortDateString();
                
                roomlist = reportingDBManager.getBookingDetailsByDates(dt1, dt2);

                gvAllGuests.DataSource = roomlist;
                gvAllGuests.DataBind();

                pnlFirstStep.Visible = false;
                pnlSecondStep.Visible = true;
                Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
                if (roomlist.Count == 0)
                {
                    ClientScriptMgr.Execute("$('#errorbox2').html('There are no guests in Delonix Regia at this date range.');$('#errorbox2').hide().show();");
                    PDFBut.Visible = false;
                    ExcelBut.Visible = false;
                    PrintBut.Visible = false;
                }
            }
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.Date > DateTime.Today)
            {
                e.Day.IsSelectable = false;
            }
        }

        protected void ExcelBut_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Charset = "";
            string FileName = "All Room Guest Report from: " + dateone  + " to " + datetwo + ".xls";
            StringWriter strwritter = new StringWriter();
            HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);

            Response.Clear();
            gvAllGuests.GridLines = GridLines.Both;
            gvAllGuests.HeaderStyle.Font.Bold = true;
            gvAllGuests.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());
            
                        
            Response.End();
        }

        protected void PDFBut_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.pdf", "All Room Guest Report from: " + dateone + " to " + datetwo));
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            
            System.IO.StringWriter stringWrite1 = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWrite1);
            DataGrid myDataGrid1 = new DataGrid();
            myDataGrid1.DataSource = roomlist;
            myDataGrid1.DataBind();
            myDataGrid1.RenderControl(htmlWrite1);
            

            StringReader sr = new StringReader("All Room Guest Report: " + stringWrite1.ToString());
            Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            pdfDoc.SetPageSize(PageSize.A4.Rotate());
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            HttpContext.Current.Response.Write(pdfDoc);
            HttpContext.Current.Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
               server control at run time. */
        }

        protected void PrintBut_Click(object sender, EventArgs e)
        {
            //gvAllGuests.AllowPaging = false;
            //gvAllGuests.DataSource = roomlist;
            //gvAllGuests.DataBind();
            //StringWriter sw = new StringWriter();
            //HtmlTextWriter hw = new HtmlTextWriter(sw);
            //gvAllGuests.RenderControl(hw);
            //string gridHTML = sw.ToString().Replace("\"", "'")
            //    .Replace(System.Environment.NewLine, "");
            //StringBuilder sb = new StringBuilder();
            //sb.Append("<script type = 'text/javascript'>");
            //sb.Append("window.onload = new function(){");
            //sb.Append("var printWin = window.open('', '', 'left=0");
            //sb.Append(",top=0,width=1000,height=600,status=0');");
            //sb.Append("printWin.document.write(\"");
            //sb.Append(gridHTML);
            //sb.Append("\");");
            //sb.Append("printWin.document.close();");
            //sb.Append("printWin.focus();");
            //sb.Append("printWin.print();");
            //sb.Append("printWin.close();};");
            //sb.Append("</script>");
            //ClientScript.RegisterStartupScript(this.GetType(), "Room Status Report", sb.ToString());
            //gvAllGuests.AllowPaging = true;
            //gvAllGuests.DataSource = roomlist;
            //gvAllGuests.DataBind();
        }  
    }
}